﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Configuration;
using PServiceBus.Core.Runtime;

namespace PServiceBus.Core.Components {
    public class ServiceMonitor : ComponentBase {
        public override void Execute() {
            var services = ServiceController.GetServices().AsEnumerable();
            services = services.Where(service => 
                service.ServiceName.ToLower().Contains("pservicebus"));
            services = services.Where(service => service.Status == ServiceControllerStatus.Stopped);
            foreach (var service in services) {
                MethodHelper.Try(() => {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);
                });
            }
        }
    }
}
